package com.hurl.waguji.web.cx;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.ScrollableResults;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.hurl.library.http.HttpUtils;
import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.CollectionUtils;
import com.hurl.waguji.base.SqlReader;
import com.hurl.waguji.base.dao.HibernateDao;
import com.hurl.waguji.base.dao.HibernateService;
import com.hurl.waguji.persistence.DmGpBO;
import com.hurl.waguji.persistence.JcsjFhrzFhbHexunBO;
import com.hurl.waguji.persistence.JcsjFhrzPgbHexunBO;
import com.hurl.waguji.persistence.JcsjCwCwbbThsBO;
import com.hurl.waguji.persistence.JcsjGbjgbThsBO;
import com.hurl.waguji.persistence.JcsjGdThsBO;
import com.hurl.waguji.persistence.JcsjGdrsThsBO;
import com.hurl.waguji.persistence.JcsjGsggThsBO;
import com.hurl.waguji.persistence.JcsjGsgkThsBO;
import com.hurl.waguji.persistence.JcsjJjsjbThsBO;
import com.opensymphony.xwork2.ActionSupport;

public class ProfileAction extends ActionSupport {
	
	private List<Map<String,String>> searchResult = new ArrayList<Map<String,String>>();
	private FormatNumberMethod formatNumber = new FormatNumberMethod();
	private RevertArrayMethod revertArray = new RevertArrayMethod();
	
	private String gpdm;
	private String term;
	private DmGpBO stock;
	
	/* 公司概况 */
	private JcsjGsgkThsBO gsgk;
	
	/* 公司高管 */
	private List<JcsjGsggThsBO> gsgglist = new ArrayList<JcsjGsggThsBO>();
	
	/* 财务 */
	private List<Map<String,Object>> zcfzblist = new ArrayList<Map<String,Object>>();
	private List<Map<String,Object>> lrblist = new ArrayList<Map<String,Object>>();
	private List<Map<String,Object>> xjlblist = new ArrayList<Map<String,Object>>();
	private List<Map<String,Object>> cwbllist = new ArrayList<Map<String,Object>>();
	private List<JcsjCwCwbbThsBO> cwbblist = new ArrayList<JcsjCwCwbbThsBO>();
	
	/* 股东股本 */
	private List<JcsjGdThsBO> gdlist = null;
	private List<JcsjGdrsThsBO> gdrslist = null;
	private List<JcsjGbjgbThsBO> gbjglist = null;
	private List<JcsjJjsjbThsBO> jjsjlist = null;
	
	/* 分红融资 */
	private List<JcsjFhrzFhbHexunBO> fhlist = null;
	private List<JcsjFhrzPgbHexunBO> pglist = null;
	
	private static final List<String> zcfzbKeyList = new ArrayList<String>();
	private static final List<String> lrbKeyList = new ArrayList<String>();
	private static final List<String> xjlbKeyList = new ArrayList<String>();
	private static final List<String> cwblKeyList = new ArrayList<String>();
	
	
	static{
		zcfzbKeyList.add("nd");
		zcfzbKeyList.add("zcze");
		zcfzbKeyList.add("ldzchj");
		zcfzbKeyList.add("fzhj");
		zcfzbKeyList.add("ldfzhj");
		zcfzbKeyList.add("gdqyhj");
		zcfzbKeyList.add("sszb");
		zcfzbKeyList.add("zbgjj");
		zcfzbKeyList.add("yygj");
		zcfzbKeyList.add("wfplr");
		zcfzbKeyList.add("zcfzl");
		zcfzbKeyList.add("gdqybl");
		
		lrbKeyList.add("nd");
		lrbKeyList.add("zyywsr");
		lrbKeyList.add("zyywcb");
		lrbKeyList.add("xsfy");
		lrbKeyList.add("cwfy");
		lrbKeyList.add("glfy");
		lrbKeyList.add("yylr");
		lrbKeyList.add("tzsy");
		lrbKeyList.add("lrze");
		lrbKeyList.add("sds");
		lrbKeyList.add("jlr");
		lrbKeyList.add("xsmll");
		

		xjlbKeyList.add("nd");
		xjlbKeyList.add("jyhdxjlr");
		xjlbKeyList.add("jyhdxjlc");
		xjlbKeyList.add("jyhdxjllje");
		xjlbKeyList.add("tzhdxjlr");
		xjlbKeyList.add("tzhdxjlc");
		xjlbKeyList.add("tzhdxjllje");
		xjlbKeyList.add("czhdxjlr");
		xjlbKeyList.add("czhdxjlc");
		xjlbKeyList.add("czhdxjllje");
		xjlbKeyList.add("hlbdyx");
		xjlbKeyList.add("xjjdjwjzje");

		cwblKeyList.add("nd");
		cwblKeyList.add("ldbl");
		cwblKeyList.add("sdbl");
		cwblKeyList.add("xjbl");
		cwblKeyList.add("qyfzbl");
		cwblKeyList.add("cqzcshl");
		cwblKeyList.add("ldzcdfzzebl");
		cwblKeyList.add("yxfzl");
		cwblKeyList.add("blzqbl");
		cwblKeyList.add("chldfzbl");
		cwblKeyList.add("zwcfbl");
		cwblKeyList.add("fzjgbl");
		cwblKeyList.add("hzbdl");
		cwblKeyList.add("gdzccbzzcbbl");
		cwblKeyList.add("lxzfbs");
		cwblKeyList.add("gdcwfybfl");
		cwblKeyList.add("qsjzbl");
		cwblKeyList.add("yyzcyzzcbl");
		cwblKeyList.add("xjyzzcbl");
		cwblKeyList.add("jzcsyl");
		cwblKeyList.add("zzcsyl");
		cwblKeyList.add("zbjsyl");
		cwblKeyList.add("zyywlrl");
		cwblKeyList.add("zysrmlrl");
		cwblKeyList.add("zysrsqlrl");
		cwblKeyList.add("zysrshlrl");
		cwblKeyList.add("kcfjcsyhjlrl");
		cwblKeyList.add("yylrl");
		cwblKeyList.add("yybl");
		cwblKeyList.add("cbfylrl");
		cwblKeyList.add("xsqjfyl");
		cwblKeyList.add("fjcxsybl");
		cwblKeyList.add("gljybl");
		cwblKeyList.add("bqglsyl");
		cwblKeyList.add("glzfbl");
		cwblKeyList.add("sylcbl");
		cwblKeyList.add("yszkzzl");
		cwblKeyList.add("yszkhsq");
		cwblKeyList.add("ldzczzl");
		cwblKeyList.add("gdzczzl");
		cwblKeyList.add("chzzl");
		cwblKeyList.add("chxsq");
		cwblKeyList.add("zzczzl");
		cwblKeyList.add("jzczzl");
		cwblKeyList.add("zylrbz");
		cwblKeyList.add("ldzcdzzcbl");
		cwblKeyList.add("zcfzl");
		cwblKeyList.add("zbhbl");
		cwblKeyList.add("zbgdhbl");
		cwblKeyList.add("zbzzl");
		cwblKeyList.add("gdzcycqfzl");
		cwblKeyList.add("gdzcygdqybl");
		cwblKeyList.add("gdzcjzl");
		cwblKeyList.add("qyxs");
		cwblKeyList.add("cqfzbl");
		cwblKeyList.add("cqbl");
		cwblKeyList.add("jzyfzbl");
		cwblKeyList.add("jzygdzcbl");
		cwblKeyList.add("yxzcjzzwl");
		cwblKeyList.add("gdqybl");
		cwblKeyList.add("zyywzzl");
		cwblKeyList.add("yskxzzl");
		cwblKeyList.add("jlrzzl");
		cwblKeyList.add("gdzctzkzl");
		cwblKeyList.add("zzckzl");
		cwblKeyList.add("mgsyzzl");
		cwblKeyList.add("jzczhzl");
		cwblKeyList.add("mgjyxjjll");
		cwblKeyList.add("zcjyxjllhbl");
		cwblKeyList.add("jlrxjhl");
		cwblKeyList.add("jyxjjlldfzbl");
		cwblKeyList.add("xjjllzzl");
		cwblKeyList.add("jyhdsyzl");
		cwblKeyList.add("zyywxjbl");
		cwblKeyList.add("xjlljgbl");
		cwblKeyList.add("mgjzc");
		cwblKeyList.add("tzhmgjzc");
		cwblKeyList.add("mgsy");
		cwblKeyList.add("kcfjcsymgsy");
		cwblKeyList.add("mgzysr");
		cwblKeyList.add("mgxjllje");
		cwblKeyList.add("mgzbgj");
	}
	
	public List<JcsjFhrzFhbHexunBO> getFhlist() {
		return fhlist;
	}

	public List<JcsjFhrzPgbHexunBO> getPglist() {
		return pglist;
	}

	public List<JcsjGdThsBO> getGdlist() {
		return gdlist;
	}

	public List<JcsjGdrsThsBO> getGdrslist() {
		return gdrslist;
	}

	public List<JcsjGbjgbThsBO> getGbjglist() {
		return gbjglist;
	}

	public List<JcsjJjsjbThsBO> getJjsjlist() {
		return jjsjlist;
	}

	public List<Map<String, Object>> getZcfzblist() {
		return zcfzblist;
	}

	public List<Map<String, Object>> getLrblist() {
		return lrblist;
	}

	public List<Map<String, Object>> getXjlblist() {
		return xjlblist;
	}

	public List<Map<String, Object>> getCwbllist() {
		return cwbllist;
	}

	public List<JcsjCwCwbbThsBO> getCwbblist() {
		return cwbblist;
	}

	public List<JcsjGsggThsBO> getGsgglist() {
		return gsgglist;
	}

	public RevertArrayMethod getRevertArray(){
		return this.revertArray;
	}
	public FormatNumberMethod getFormatNumber(){
		return this.formatNumber;
	}

	public JcsjGsgkThsBO getGsgk() {
		return gsgk;
	}

	public void setGsgk(JcsjGsgkThsBO gsgk) {
		this.gsgk = gsgk;
	}

	public DmGpBO getStock() {
		return stock;
	}

	public void setStock(DmGpBO stock) {
		this.stock = stock;
	}

	public List<Map<String, String>> getSearchResult() {
		return searchResult;
	}

	public void setSearchResult(List<Map<String, String>> searchResult) {
		this.searchResult = searchResult;
	}

	public String getTerm() {
		return term;
	}

	public void setTerm(String term) {
		this.term = term;
	}

	public String getGpdm() {
		return gpdm;
	}

	public void setGpdm(String gpdm) {
		this.gpdm = gpdm;
	}

	public String search()throws Exception{
		if(StringUtils.isNotBlank(term)){
			String sql = SqlReader.getSql("SQL000004",new String[]{term,term,HttpUtils.decodeUrl(term,"UTF-8")});
			HibernateDao dao = HibernateService.getDao();
			ScrollableResults results = dao.queryBySQL(sql).setReadOnly(true).scroll();
			while(results.next()){
				Object[] row = results.get();
				Map<String,String> map = new HashMap<String,String>();
				map.put("value", (String)row[0]);
				String label = StringUtils.fixPlace((String)row[1],10," ");
				label+=StringUtils.fixPlace((String)row[3],10," ");
				label+=StringUtils.fixPlace((String)row[2],10," ");
				map.put("label", label);
				searchResult.add(map);
			}
		}
		return SUCCESS;
	}
	
	public String query()throws Exception{
		if(StringUtils.isNotBlank(this.gpdm)){
			HibernateDao dao = HibernateService.getDao();
			stock = (DmGpBO)dao.load(DmGpBO.class, gpdm);
		}
		return SUCCESS;
	}
	
	public String hq()throws Exception{
		if(StringUtils.isNotBlank(this.gpdm)){
			HibernateDao dao = HibernateService.getDao();
			stock = (DmGpBO)dao.load(DmGpBO.class, gpdm);
		}
		return SUCCESS;
	}
	public String basic()throws Exception{
		if(StringUtils.isNotBlank(this.gpdm)){
			HibernateDao dao = HibernateService.getDao();
			stock = (DmGpBO)dao.load(DmGpBO.class, gpdm);
			gsgk = (JcsjGsgkThsBO)dao.load(JcsjGsgkThsBO.class, this.gpdm);
			if(!StringUtils.isBlank(gsgk.getZyyw())){
				gsgk.setZyyw(gsgk.getZyyw().replace("\r", "<p>"));
			}
			if(!StringUtils.isBlank(gsgk.getGsjj())){
				gsgk.setGsjj(gsgk.getGsjj().replace("\r", "<p>"));
			}
			if(!StringUtils.isBlank(gsgk.getLsyg())){
				gsgk.setLsyg(gsgk.getLsyg().replace("\r", "<p>"));
			}
		}

		return SUCCESS;
	}
	public String gsgg()throws Exception{
		if(StringUtils.isNotBlank(this.gpdm)){
			HibernateDao dao = HibernateService.getDao();
			ScrollableResults results = dao.createCriteria(JcsjGsggThsBO.class).add(Restrictions.eq("id.gpdm", this.gpdm)).addOrder(Order.asc("rqq")).scroll();
			while(results.next()){
				gsgglist.add((JcsjGsggThsBO)results.get(0));
			}
		}
		return SUCCESS;
	}
	public String cw()throws Exception{
		
		if(StringUtils.isNotBlank(this.gpdm)){
			HibernateDao dao = HibernateService.getDao();
			
			List<Object[]> zcfzbarray = (List<Object[]>)dao.queryBySQL(SqlReader.getSql("SQL000005",new String[]{this.gpdm})).list();
			if(zcfzbarray!=null && !zcfzbarray.isEmpty()){
				for(Object[] array : zcfzbarray){
					zcfzblist.add(CollectionUtils.array2Map(array, zcfzbKeyList));
				}
				Collections.reverse(zcfzblist);
			}
			
			List<Object[]> lrbarray = (List<Object[]>)dao.queryBySQL(SqlReader.getSql("SQL000006",new String[]{this.gpdm})).list();
			if(lrbarray!=null && !lrbarray.isEmpty()){
				for(Object[] array : lrbarray){
					lrblist.add(CollectionUtils.array2Map(array, lrbKeyList));
				}
				Collections.reverse(lrblist);
			}
			
			List<Object[]>xjlbarray = (List<Object[]>)dao.queryBySQL(SqlReader.getSql("SQL000007",new String[]{this.gpdm})).list();
			if(xjlbarray!=null && !xjlbarray.isEmpty()){
				for(Object[] array : xjlbarray){
					xjlblist.add(CollectionUtils.array2Map(array, xjlbKeyList));
				}
				Collections.reverse(xjlblist);
			}
			
			List<Object[]> cwblarray = (List<Object[]>)dao.queryBySQL(SqlReader.getSql("SQL000008",new String[]{this.gpdm})).list();
			if(cwblarray!=null && !cwblarray.isEmpty()){
				for(Object[] array : cwblarray){
					cwbllist.add(CollectionUtils.array2Map(array, cwblKeyList));
				}
				Collections.reverse(cwbllist);
			}
			
			cwbblist = (List<JcsjCwCwbbThsBO>)dao.createCriteria(JcsjCwCwbbThsBO.class).add(Restrictions.eq("id.gpDm", this.gpdm)).addOrder(Order.desc("id.mc")).list();
			
			return SUCCESS;
		}
		return SUCCESS;
	}
	public String gdgb()throws Exception{
		if(StringUtils.isNotBlank(this.gpdm)){
			HibernateDao dao = HibernateService.getDao();
			/* 流通股东 */
			Date rq = null;
			ScrollableResults results = dao.queryBySQL(SqlReader.getSql("SQL000009", new String[]{this.gpdm})).scroll();
			if(results.next()){
				rq = (Date)results.get(0);
			}
			if(rq!=null){
				gdlist = (List<JcsjGdThsBO>)dao.createCriteria(JcsjGdThsBO.class).add(Restrictions.eq("gpDm", this.gpdm)).add(Restrictions.eq("rq", rq)).addOrder(Order.desc("cgs")).setFetchSize(10).list();
			}else{
				gdlist = new ArrayList<JcsjGdThsBO>();
			}
			
			/* 股东人数 */
			gdrslist = (List<JcsjGdrsThsBO>)dao.createCriteria(JcsjGdrsThsBO.class).add(Restrictions.eq("id.gpDm", this.gpdm)).addOrder(Order.desc("id.rq")).setFetchSize(10).list();
			Collections.reverse(gdrslist);
			
			/* 股本结构 */
			gbjglist = (List<JcsjGbjgbThsBO>)dao.createCriteria(JcsjGbjgbThsBO.class).add(Restrictions.eq("id.gpDm", this.gpdm)).addOrder(Order.desc("id.rq")).setFetchSize(10).list();
			Collections.reverse(gbjglist);
			
			/* 解禁时间表 */
			jjsjlist = (List<JcsjJjsjbThsBO>)dao.createCriteria(JcsjJjsjbThsBO.class).add(Restrictions.eq("id.gpDm", this.gpdm)).addOrder(Order.asc("id.jjr")).list();
		}
		return SUCCESS;
	}
	public String fhrz()throws Exception{
		if(StringUtils.isNotBlank(this.gpdm)){
			HibernateDao dao = HibernateService.getDao();
			
			/* 分红表 */
			fhlist = (List<JcsjFhrzFhbHexunBO>)dao.createCriteria(JcsjFhrzFhbHexunBO.class).add(Restrictions.eq("id.gpdm", this.gpdm)).addOrder(Order.asc("id.ggr")).list();
			
			/* 配股表 */
			pglist = (List<JcsjFhrzPgbHexunBO>)dao.createCriteria(JcsjFhrzPgbHexunBO.class).add(Restrictions.eq("id.gpdm", this.gpdm)).addOrder(Order.asc("id.ggr")).list();
		}
		return SUCCESS;
	}
}
